Skip to content

feat(aws): prefer an AWS SSO login over stale shared-file keys - #945

Open
ArangoGutierrez wants to merge 13 commits into
NVIDIA:mainfrom
ArangoGutierrez:feat/aws-sso-profile
Open

ArangoGutierrez wants to merge 13 commits into
NVIDIA:mainfrom
ArangoGutierrez:feat/aws-sso-profile

Conversation

@ArangoGutierrez

Copy link
Copy Markdown
Collaborator

Problem

With an AWS SSO login, holodeck got a 401: the Go SDK lets static keys in
~/.aws/credentials shadow SSO, while the aws CLI prefers SSO. Expired sessions
surfaced raw SDK errors.

Change

  • internal/aws.LoadConfig (used by aws.New, cleanup.New, os ami) prefers
    SSO, as the aws CLI does, when a profile has SSO plus shared-file keys. Env
    keys, an explicit credentials file, role_arn and web identity keep today's
    behavior. One warning per process, on stderr. Credentials stay lazy.
  • Expired, missing or revoked sessions print
    run "aws sso login --profile <name>".
  • Every test package that can load AWS config runs isolated: temp HOME, no
    credentials, proxies at a closed port.

Testing

Offline precedence and hint tests, mutation-verified. Hermetic build, vet,
go test -race, golangci-lint and go mod tidy -diff pass. Real SSO login:
401 before, AMI after.

Nobody had run holodeck with an AWS SSO profile, so these tests prove the
SDK default chain resolves both legacy and sso-session profiles, and pin
the exact hint users should see when the SSO session is expired or was
never started.

The real-SDK cases drive an SSM call through config.LoadDefaultConfig
under a fake HOME and token cache; credential resolution fails locally
before any request is signed. The provider test guards New against a
future change that replaces the default chain, without calling Retrieve,
so New stays lazy for holodeck list.

SSOHint is a pass-through stub here; the hint cases are expected to fail.
go.mod moves aws-sdk-go-v2/credentials to a direct requirement because
the tests import ssocreds.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
…expired

With AWS_PROFILE naming an SSO profile, the SDK default chain already
resolves credentials, but an expired or never-started session surfaced
as a raw SDK error that does not say how to recover. The single CLI
error exit now prefixes it with the exact "aws sso login --profile X"
command, naming the profile the SDK used (AWS_PROFILE, else
AWS_DEFAULT_PROFILE), and keeps the original error in the chain.

Legacy profiles fail with the typed ssocreds.InvalidTokenError, but
sso-session profiles, the layout "aws configure sso" writes, fail with
untyped token-cache errors. Both are matched; the real-SDK tests pin the
SDK text so an upgrade that changes it goes red.

Credentials stay lazy: nothing is fetched in aws.New, so holodeck list
still works with an expired session.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
SSO users had no documented path: the prerequisites listed only static
keys and the shared credentials file. Show the aws sso login, export
AWS_PROFILE, holodeck create flow, and warn that holodeck does not
record the profile, so delete, status and update must run with the same
AWS_PROFILE (the SSM transport inherits it too).

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
A stale static key in ~/.aws/credentials shadows an SSO login for Go
programs: the SDK resolves shared-file static keys before SSO, while the
aws CLI does the opposite. These cases pin the intended precedence:
prefer SSO only when the profile has both and nothing explicit (env keys,
an explicit AWS_SHARED_CREDENTIALS_FILE) says otherwise, and never warn
falsely when the keys live in the config file.

LoadConfig is a stub with today's behaviour, so the two prefer-SSO cases
fail. It already resolves the shared file paths at call time: the SDK's
DefaultSharedConfigFiles are computed at package init from the real
HOME, which would let a test read the developer's own ~/.aws.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
The AWS SDK computes DefaultSharedConfigFiles and
DefaultSharedCredentialsFiles once, at package init, from HOME. A
t.Setenv("HOME") in a test comes too late for them: any code path that
falls back to those defaults reads the developer's real ~/.aws. That
happened once while building this change, when the call sites were
temporarily reverted to raw LoadDefaultConfig for a RED check.

testutil.RunWithIsolatedHome re-runs a test in a child process whose
HOME is an empty temp dir from process start, and fails if the child
fails or does not run the test. Every test that loads SDK config now
runs that way, with its fixtures at the isolated HOME, so even a
regressed call site reads the fixture and never the real ~/.aws.

The new cases in aws.New and cleanup.New fail while those call sites
still use LoadDefaultConfig, so they guard the wiring of the
prefer-SSO rule, not only the rule itself.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
gosec's taint analysis flags every file path built from the
isolated-HOME environment variable. testutil.IsolatedHome now owns that
lookup and the per-test reset of ~/.aws, so the path sink lives in one
reviewed place and the fixture setup is not repeated in three packages.
The fake env credentials in the precedence table get a G101 annotation.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
…s CLI does

The Go SDK resolves static keys from the shared credentials file before
an SSO configuration in the same profile; the aws CLI does the opposite.
A stale [default] key in ~/.aws/credentials therefore made holodeck fail
with a 401 AuthFailure while "aws sts get-caller-identity" succeeded
through SSO.

internalaws.LoadConfig keeps the SDK default chain, and when the active
profile has both an SSO configuration and static keys, with no static
keys in the environment and no explicit AWS_SHARED_CREDENTIALS_FILE, it
reloads without the shared credentials file and logs one warning. It
uses that result only if the SDK really resolved the SSO provider; any
error, or any other outcome, falls back to the unchanged default load
with no warning. Credentials stay lazy.

The shared file paths are resolved at call time instead of from the
SDK's package-init defaults; for a real process they are the same paths,
and the profile check and the load read the same files.

aws.New, cleanup.New and "holodeck os ami" now load through LoadConfig.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
…l account

Tests in cmd/cli/{create,dryrun,cleanup} built real AWS clients with
whatever credentials the machine had and passed only because those
credentials failed. Once an SSO login works, a full `go test` run
created a real VPC in a shared account. Make every test binary that can
reach AWS hermetic by construction.

testutil.RunAWSIsolated (called from TestMain) re-executes the test
binary with HOME set to a new temp dir, AWS_CONFIG_FILE and
AWS_SHARED_CREDENTIALS_FILE set to empty files in it, every credential
or profile variable the SDK reads removed, IMDS disabled and
AWS_ENDPOINT_URL set to a closed local port. A re-exec is required: the
SDK computes its default config paths from HOME at package init, before
TestMain runs. TestAWSIsolation in each package checks all of that,
including the SDK's init-time paths, before touching the SDK, then
requires that resolving credentials fails without naming a real file.

Census (packages whose test binaries can reach a real AWS client):
- dynamic, hermetic coverage of internalaws.LoadConfig (every production
  config load): internal/aws 94.4% (positive control), pkg/cleanup
  94.4%, pkg/provider/aws 94.4%, cmd/cli/{cleanup,create,dryrun} 66.7%,
  internal/instances 66.7%; SSMTransport.DialContext (aws CLI exec) 0%
  everywhere; api/holodeck/v1alpha1 n/a (negative control).
- static, link graph: test binaries linking internal/aws or
  pkg/provisioner, which also covers paths only an empty HOME hides
  (for example the default ~/.cache/holodeck instance cache): the seven
  above plus pkg/provisioner, pkg/utils, cmd/cli, cmd/cli/{common,delete,
  describe,list,scp,ssh,status,update} and internal/aws/awsfake.
All 19 get the TestMain and the guard. No test needed real AWS; the
real-AWS e2e suite under tests/ is unchanged.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
The critic gate on 229caf1 found two blocking and five should-fix
problems. These tests encode each one before the fix:

- holodeck list must stay machine-readable: LoadConfig called three
  times warns once, on the logger's stderr writer, never on stdout.
- The precedence table now pins EXACT SDK credential sources and the
  warning count for P1-P8 and the critic's cases. Following the aws
  CLI order, credential_process (A2) and a container URI (A7) flip to
  SSO; assume-role in either file (A1, A1b, A1c), web identity (A4, A6),
  credential_source (A5) and a missing profile (A9) do not. A1b fails
  today: the SSO reload drops a role_arn that lives in the credentials
  file.
- SSOHint names the profile for a revoked session, the SSO service's
  typed UnauthorizedException.
- The isolated child must route AWS hosts, including the SSO portal that
  AWS_ENDPOINT_URL does not cover, to a closed proxy, while loopback
  stays direct; and a stale isolation marker must stop a test binary
  before any test runs.
- holodeck os ami has a wiring test like aws.New and cleanup.New.

ResetPreferSSOWarning needs the once variable, declared here unused.
go.mod moves service/sso to a direct requirement for the typed error.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
Review fixes on top of the prefer-SSO change:

- holodeck list loads one config per cached AWS instance, and the
  prefer-SSO warning went to stdout each time, breaking list -o json
  and -q. It is now printed once per process, to the logger's Out
  writer (os.Stderr by default), regardless of quiet mode.
- The SSO reload reads no credentials file, so it dropped a role_arn
  that lives there and signed as the bare SSO identity. A profile with
  role_arn no longer flips.
- SSOHint also recognises the SSO service's UnauthorizedException, the
  error for a revoked or server-invalidated session.
- The isolated test child now routes HTTP(S) through a closed local
  proxy. AWS_ENDPOINT_URL alone never covered the SSO and SSO-OIDC
  clients the credential chain builds. Loopback stays direct, and
  NO_PROXY is set to exactly the public-IP lookup hosts of
  utils.DefaultIPServices (none is an AWS host): four existing
  pkg/provider/aws tests call utils.GetIPAddress, and a strict proxy
  would have changed their behaviour. A pkg/utils test keeps the two
  host lists equal. The guard checks the proxy settings and that the
  SSO portal really resolves to the closed proxy.
- A stale HOLODECK_TEST_ISOLATED_HOME no longer runs tests un-isolated:
  the child refuses to start when the isolation checks fail.
- RunWithIsolatedHome is removed; every package that used it has the
  isolating TestMain, so it had become a no-op. Its IsolatedHome test
  moves to isolated_home_test.go.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
The prerequisites still said Holodeck simply uses the SDK default chain.
Explain the one difference, which matches the aws CLI: SSO wins over
static keys in the shared credentials file for the same profile, with a
single warning on stderr, while env keys, an explicit
AWS_SHARED_CREDENTIALS_FILE and assume-role profiles keep the SDK
behaviour.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
Go reads HTTPS_PROXY or https_proxy, but Python and the aws CLI read
the lowercase spellings, so a subprocess of a test could still use the
parent shell's corporate proxy. The isolated baseline now has both
spellings at the closed port (no_proxy equal to the IP-lookup
allowlist), and a parent's lowercase https_proxy must be reported. This
fails until the isolation sets the lowercase variables.

The "IP lookup hosts are not AWS" check now matches hostname suffixes
(amazonaws.com, aws.amazon.com, amazonaws.com.cn, aws) instead of
substrings.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
Python and the aws CLI read https_proxy, http_proxy and no_proxy, not
the uppercase spellings Go prefers, so a subprocess of an isolated test
could still reach the network through the parent shell's proxy. The
child now sets both spellings to the closed local port, and both
no-proxy spellings to exactly the IP-lookup allowlist; the guard checks
all six.

Signed-off-by: Carlos Eduardo Arango Gutierrez <eduardoa@nvidia.com>
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 46167385397

Coverage increased (+1.1%) to 52.951%

Details

  • Coverage increased (+1.1%) from the base build.
  • Patch coverage: 81 uncovered changes across 4 files (157 of 238 lines covered, 65.97%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
pkg/testutil/aws_isolation.go 153 79 51.63%
pkg/testutil/isolated_home.go 13 9 69.23%
internal/aws/config.go 51 49 96.08%
cmd/cli/main.go 1 0 0.0%
Total (8 files) 238 157 65.97%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 12181
Covered Lines: 6450
Line Coverage: 52.95%
Coverage Strength: 0.53 hits per line

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants